import { ActionContext } from 'vuex'
import * as $api from '@/api/user'
import { local_storage, session_storage } from '@/utils/storage'
import { IPermissionState } from '@/store/modules/permission'
import { IStore } from '../types'
import { LoginParams, CaptchaCheckParams, CaptchaGetParams, SmsCodeParams } from '@/api/model/userModel'
import { TOKEN_LOGIN_KEY, COMPLAIN_PROMPT_KEY } from '@/enums/cacheEnum'
import { captchaCheck, captchaGet, getSmsCode, getPddSmsCode, delegateLogin } from '@/api/user'

export type IUserInfo = {
  accountId: number
  userName: string
  accountName: string
  accountMobile: string
  authorizationList: string[]
}
export type IUserState = {
  token: string
  userInfo: IUserInfo
}

const state = {
  token: local_storage.get(TOKEN_LOGIN_KEY) || null,
  userInfo: {
    accountId: null,
    userName: '',
    authorizationList: []
  }
}

const mutations = {
  SET_TOKEN: (state: IUserState, token: string) => {
    state.token = token
  },
  SET_USERINFO: (state: IUserState, userInfo: IUserInfo) => {
    state.userInfo = userInfo
  }
}

const actions = {
  // 扫码登录
  async scanCodeLogin({ commit }: ActionContext<IUserState, IStore>, params: LoginParams) {
    return new Promise((resolve, reject) => {
      $api
        .login(params)
        .then((res) => {
          const { data } = res
          commit('SET_TOKEN', data)
          local_storage.set(TOKEN_LOGIN_KEY, data)
          resolve(res)
        })
        .catch(() => {
          reject()
        })
    })
  },
  // 获取用户信息
  getUserInfo({ commit }: ActionContext<IPermissionState, IStore>) {
    return new Promise((resolve, reject) => {
      $api
        .getUserInfo()
        .then((res) => {
          commit('SET_USERINFO', res.data)
          resolve(res.data)
        })
        .catch(() => {
          reject()
        })
    })
  },
  // 退出，清除用户信息
  logout({ commit }: ActionContext<IPermissionState, IStore>) {
    return new Promise((resolve) => {
      commit('SET_TOKEN', null)
      commit('SET_USERINFO', null)
      // 清除本地所有缓存
      session_storage.clear()
      // 清除本地所有缓存
      local_storage.remove(TOKEN_LOGIN_KEY)
      local_storage.remove(COMPLAIN_PROMPT_KEY)
      local_storage.remove('LOCAL_ROOT_ROUTER_TYPE')
      resolve(null)
    })
  },
  async captchaGet({ commit }: ActionContext<IUserState, IStore>, params: CaptchaGetParams) {
    return await captchaGet(params)
  },
  async captchaCheck({ commit }: ActionContext<IUserState, IStore>, params: CaptchaCheckParams) {
    return await captchaCheck(params)
  },
  async getSmsCode({ commit }: ActionContext<IUserState, IStore>, params: SmsCodeParams) {
    return await getSmsCode({ ...params, platformType: 1 })
  },
  async getPddSmsCode({ commit }: ActionContext<IUserState, IStore>, params: SmsCodeParams) {
    return await getPddSmsCode({ ...params, platformType: 1 })
  },
  async delegateLogin({ commit }: ActionContext<IUserState, IStore>, params: any) {
    const res = await delegateLogin(params)
    return res
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
